package com.sskj.givecoin.v.activity;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sskj.givecoin.MyAppLication;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.App;
import com.sskj.givecoin.common.ClassicsHeader;
import com.sskj.givecoin.common.Constants;
import com.sskj.givecoin.common.adapter.BaseAdapter;
import com.sskj.givecoin.common.adapter.ViewHolder;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.AseetListBean;
import com.sskj.givecoin.m.bean.UserInfo;
import com.sskj.givecoin.utils.SPUtils;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import java.text.DecimalFormat;

import butterknife.BindView;

/**
 * ProjectName：bfex
 * DESC: (资产管理)
 * Created by 李岩 on 2018/6/29 0029
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class AssetManageActivity extends BaseActivity<AssetManagePresenter> implements View.OnClickListener, OnRefreshListener {
    @BindView(R.id.asset_manage_toolbar)
    ToolBarLayout mAssetManageToolbar;
    @BindView(R.id.asset_manage_recyclerview)
    RecyclerView mAssetManageRecyclerview;
    @BindView(R.id.asset_manage_smartrefresh)
    SmartRefreshLayout mAssetManageSmartrefresh;
    private BaseAdapter<AseetListBean.AseetBean> mAdapter;
    private TextView mAssetBalance;
    DecimalFormat mFormat = new DecimalFormat("0.0000");


    private UserInfo mUserInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_asset_manage;
    }

    @Override
    public AssetManagePresenter getPresenter() {
        return new AssetManagePresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        mAssetManageToolbar.setLeftButtonOnClickLinster(this);
        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        classicsHeader.setBackgroundColor(getResources().getColor(R.color.background));
        mAssetManageSmartrefresh.setRefreshHeader(classicsHeader);
        mAssetManageSmartrefresh.setOnRefreshListener(this);

        mAssetManageRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAssetManageRecyclerview.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        mAdapter = new BaseAdapter<AseetListBean.AseetBean>(R.layout.item_asset_manage, null, mAssetManageRecyclerview) {
            @Override
            public void bind(ViewHolder holder, AseetListBean.AseetBean item) {
                holder.setText(R.id.item_asset_manage_type, item.pname);
                holder.setText(R.id.item_asset_manage_available_balance, mFormat.format(item.usable));
                holder.setText(R.id.item_asset_manage_frozen_capital, mFormat.format(item.frost));
                holder.setOnClickListener(R.id.item_asset_manage_recharge, v -> {//充值
                    startActivity(new Intent(mActivity, RechargeActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid).putExtra(Constants.INTENT_ASSET_NAME, item.pname));
                });
                holder.setOnClickListener(R.id.item_asset_manage_withdraw, v -> {//提币
                    if (checkIdent()) {

                        if (checkPwdStatus()) {
                            startActivity(new Intent(mActivity, WithdrawActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid).putExtra(Constants.INTENT_ASSET_NAME, item.pname));
                        }

                    }
                });
                holder.setOnClickListener(R.id.item_asset_manage_record, v -> {//账户明细
                    startActivity(new Intent(mActivity, CapitalDetailsActivity.class).putExtra(Constants.INTENT_ASSET_ID, item.pid));
                });
            }
        };
//        View headerView = LayoutInflater.from(this).inflate(R.layout.layout_asset_header, null);
//        mAssetBalance = headerView.findViewById(R.id.asset_balance);
//        mAdapter.addHeaderView(headerView);
        mPresenter.requestUserAssetList();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        mAssetManageSmartrefresh.finishRefresh();
        mPresenter.requestUserAssetList();
    }


    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.upDateUserInfo(MyAppLication.getMobile());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftbutton:
                finish();
                break;
        }
    }

    public void onDataSuccess(AseetListBean data) {
//        mAssetBalance.setText(data.wallone);
        mAdapter.setNewData(data.asset);
    }

    public void updateUserInfo(UserInfo user) {
        if (user != null) {
            mUserInfo = user;
            SPUtils.putBean(mActivity, Constants.SP_USER_INFO, user);
        }
    }


    public boolean checkPwdStatus() {
        if (mUserInfo != null) {
            if (mUserInfo.tpwd != null && mUserInfo.tpwd.equals("1")) {
                return true;
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strTransferActivity206));
                startActivity(new Intent(mActivity, SecurityCenterActivity.class));
                return false;
            }
        }
        return false;
    }

}
